package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.EmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @GetMapping
    @ApiOperation(value = "查询所有的部门")
    public AjaxResult list(){
        List<Employee> all = employeeService.findAll();
        return AjaxResult.createSuccess(all);
    }
    @PostMapping
    @ApiOperation(value = "新增修改部门，新增没有id，修改有id") //swagger API
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    @ApiOperation(value = "根据id删除部门对象") //swagger API
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            employeeService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }

    }

    @GetMapping("/findManager")
    @ApiOperation(value = "查询所有的员工")
    public AjaxResult findManager(){
        List<Employee> all = employeeService.findAll();
        return AjaxResult.createSuccess(all);
    }
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询员工")
    public PageList<Employee> findPage(@RequestBody EmployeeQuery query){

        return employeeService.findPage(query);
    }
}
